Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to create levels (was: Introduce a createLevel call, document, add tests) #465

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

aahoughton
Copy link

This is a -- possibly stopgap -- solution to the need for levels beyond
the default set. It's imperfect in re: dtrace support, but it's also
self-contained and easy to understand.

Caveats:

  • There's no way to dynamically add dtrace probes after-the-fact
    of logger creation, so there's no way to trace levels in loggers
    that were instantiated prior to the level creation
  • I think this is completely safe in-re: Logger fields; we may be
    adding functions to the prototype left-and-right, but they'll be
    shadowed by the actual Logger instance, and we won't overwrite
    anything that exists
  • I'm a little uncomfortable with all the hand-holding /
    don't-shoot-yourself-in-the-foot going on in the createLogger
    call, but it's certainly safer
  • The documentation could be better

the default set. It's imperfect in re: dtrace support, but it's also
self-contained and easy to understand.

Caveats:

  - There's no way to dynamically add dtrace probes after-the-fact
    of logger creation, so there's no way to trace levels in loggers
    that were instantiated prior to the level creation
  - I think this is completely safe in-re: Logger fields; we may be
    adding functions to the prototype left-and-right, but they'll be
    shadowed by the actual Logger instance, and we won't overwrite
    anything that exists
  - I'm a little uncomfortable with all the hand-holding /
    don't-shoot-yourself-in-the-foot going on in the createLogger
    call, but it's certainly safer
  - The documentation could be better
@DanHulton
Copy link

This actually very nicely solves a problem I currently have - I have events that are infrequent, but important, and I want to surface them above the regular "info" logging I do to allow for postmortem debugging. I could prepend each message with a custom identifier, but a separate "notice" loglevel is a much more efficient way of going about it.

@Binarytales
Copy link

👍 in this. It would be really good for us to be able to configure Bunyan to match the Google Stackdriver levels. https://cloud.google.com/logging/docs/api/reference/rest/v2/LogEntry#LogSeverity

@aaronwbrown
Copy link

It would be great if this were integrated into Bunyan. We have the need for a logger.security({...}) to centralize security logic for easier log querying. 🎆

@gmadole
Copy link

gmadole commented May 19, 2017

👍 Would love this for something like logger.metric({...})

@eryon
Copy link

eryon commented Jul 19, 2017

@trentm can this get looked at? Great add 👍

@Epitrochoid
Copy link

I am also running into a need for this with logger.success and logger.performance and it looks like I'll have to use some hacky methods to get around it.

@williamkeller
Copy link

This would be very useful.

@vancouverwill
Copy link

this would be super useful, end update on this?

@g-wilson
Copy link

g-wilson commented Feb 6, 2018

This would be very very useful.

In the meantime, here's our workaround. Warning: h4x incoming.


So if you pass an object to the log methods it is possible to override the defaults for the final log output. You can override the level property. Which means if you set your level to say 34 in the createLogger function then you can output logs at level 35. in the console (bunyan CLI), the level looks like "LVL35:" coloured white.

logger.fatal({ level: 35, foo: 'bar' }, 'My message')
// outputs { ... "level":35,"baz":"bing","msg":"foo bar" ... }

Next step: monkey patch.

const logger = require('bunyan').createLogger({ name: 'myLogger', level: 34 })

logger.notice = (msg, ...args) => {
  if (typeof msg === 'string') {
    logger.fatal({ level: 35 }, msg, ...args)
  }
  else if (typeof msg === 'object') {
    logger.fatal({ level: 35, ...msg }, ...args)
  }
  else {
    throw new Error('Invalid arguments provided')
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.